package com.etcbase.dcs.utils.gui;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

public class LayoutWidthHeightAnimator {

	private int durationTime;
	private ViewGroup viewGroupLayout;
	private AnimatorListener userAnimatorListener;

	

	private int startWidth, stopWidth;
	private int startHeight, stopHeight;
	private int widthDifference, heightDifference;

	public String Name;

	public LayoutWidthHeightAnimator(ViewGroup viewGroupLayout, int durationTime) {
		this.viewGroupLayout = viewGroupLayout;
		this.durationTime = durationTime;
	}
	// pass -1 for the parameter you do NOT want to change during the animation
	public void start(int stopWidth, int stopHeight,
			AnimatorListener userAnimatorListener) {
		this.userAnimatorListener = userAnimatorListener;
		startWidth = viewGroupLayout.getWidth();
		startHeight = viewGroupLayout.getHeight();
		this.stopWidth = stopWidth;
		this.stopHeight = stopHeight;
		widthDifference = stopWidth - startWidth;
		heightDifference = stopHeight - startHeight;

		ValueAnimator valueAnimator = new ValueAnimator();
		valueAnimator.setDuration(durationTime);
		valueAnimator.setIntValues(new int[] { 0, 0 }); // the values are irrelevant
		valueAnimator.addUpdateListener(userAnimatorUpdateListener);
		valueAnimator.addListener(animatorListener);
		valueAnimator.start();
	}

	private AnimatorUpdateListener userAnimatorUpdateListener = new AnimatorUpdateListener() {
		@Override
		public void onAnimationUpdate(ValueAnimator valueAnimator) {
			LayoutParams layoutParams = viewGroupLayout.getLayoutParams();
			if (stopWidth >= 0) {
				layoutParams.width = (int) (startWidth + widthDifference
						* valueAnimator.getCurrentPlayTime() / durationTime);
			}

			if (stopHeight >= 0) {
				layoutParams.height = (int) (startHeight + heightDifference
						* valueAnimator.getCurrentPlayTime() / durationTime);
			}

			viewGroupLayout.setLayoutParams(layoutParams);

		}
	};

	private AnimatorListener animatorListener = new AnimatorListener() {
		@Override
		public void onAnimationStart(Animator animator) {
			if (userAnimatorListener != null)
				userAnimatorListener.onAnimationStart(animator);
		}

		@Override
		public void onAnimationRepeat(Animator animator) {
			if (userAnimatorListener != null)
				userAnimatorListener.onAnimationRepeat(animator);
		}

		@Override
		public void onAnimationEnd(Animator animator) {
			// set final values for the dimensions of the layout
			ViewGroup.LayoutParams layoutParams = viewGroupLayout.getLayoutParams();
			if (stopWidth >= 0) {
				layoutParams.width = stopWidth;
			}

			if (stopHeight >= 0) {
				layoutParams.height = stopHeight;
			}

			Log.d("**** ANIM", Name + ": final height = " + stopHeight);

			viewGroupLayout.setLayoutParams(layoutParams);

			// then call the users listener
			if (userAnimatorListener != null)
				userAnimatorListener.onAnimationEnd(animator);
		}

		@Override
		public void onAnimationCancel(Animator animator) {
			if (userAnimatorListener != null)
				userAnimatorListener.onAnimationCancel(animator);
		}
	};
}